package com.tobby.siia.fundamentals;

import javax.xml.transform.Source;

import org.springframework.ws.client.core.WebServiceOperations;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;

public class BookingServiceWithInjection {

	private final BookingDao bookingDao;

	private final WebServiceOperations mealPreferenceWebServiceInvoker;

	public BookingServiceWithInjection(BookingDao bookingDao, WebServiceOperations mealPreferenceWebServiceInvoker) {
		this.bookingDao = bookingDao;
		this.mealPreferenceWebServiceInvoker = mealPreferenceWebServiceInvoker;
	}

	public void updateMeal(MealPreference mealPreference) {
		Booking booking = bookingDao.getBookingById(mealPreference.getBookingId());
		Source mealUpdateSource = buildMealPreferenceUpdateRequest(booking, mealPreference);
		StringResult result = new StringResult();
		mealPreferenceWebServiceInvoker.sendSourceAndReceiveToResult(mealUpdateSource, result);
	}

	private Source buildMealPreferenceUpdateRequest(Booking booking, MealPreference mealPreference) {
		return new StringSource("<upateMealPreference><flightRef>" + booking.getFlightRef()
				+ "</flightRef><mealPreference>" + mealPreference + "</mealPreference></updateMealPreference>");
	}

}
